﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using MLM.Data;
namespace MLM.AdminCP.Pages
{

    [ScriptService]
    public partial class StatiticsUserRecommend : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // EndDate.SelectedDate = DateTime.Now;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string GetListDate(string UserID)
        {
            List<listPersonObject> _listObject = new List<listPersonObject>();

            List<long> listUser = new List<long>();
            List<int> _listValue = new List<int>();
            if (UserID != "undefined")
            {
                long ID = long.Parse(UserID);
                using (var DataContext = new MLMDataContext())
                {
                    var query = DataContext.Users.Where(p => p.UserId == ID);
                    if (query != null)
                    {
                        foreach (var item in query.ToList())
                        {
                            listUser.Add(item.UserId);
                        }
                    }
                    foreach (var item in listUser)
                    {

                        int _value = DataContext.Users.Count(p => p.SponsorID == item);
                        _listValue.Add(_value);
                    }
                }
            }
            else {
                using (var DataContext = new MLMDataContext())
                {
                    var query = DataContext.Users;
                    if (query != null)
                    {
                        foreach (var item in query.ToList())
                        {
                            listUser.Add(item.UserId);
                        }
                    }
                    foreach (var item in listUser)
                    {

                        int _value = DataContext.Users.Count(p => p.SponsorID == item);
                        _listValue.Add(_value);
                    }
                }
            }
            _listObject.Add(new listPersonObject { User = listUser, value = _listValue });
            JavaScriptSerializer ser = new JavaScriptSerializer();
            return ser.Serialize(_listObject);
        }

    }

    public class listPersonObject
    {
        public List<long> User { get; set; }
        public List<int> value { get; set; }
    }

}